package com.xj.admin.bussiness.insure.web;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.plugins.Page;
import com.feilong.core.Validator;
import com.feilong.core.bean.ConvertUtil;
import com.xj.admin.base.index.web.BaseController;
import com.xj.admin.bussiness.insure.service.ITbInsureService;
import com.xj.admin.util.dtgrid.model.Pager;
import com.xj.common.base.common.bean.AbstractBean;
import com.xj.common.base.common.exception.EnumSvrResult;
import com.xj.common.base.common.util.JsonUtil;
import com.xj.common.bussiness.insure.entity.TbInsure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *     保单管理 <br/>
 *     前端控制器
 * </p>
 * @author zhaohai
 * @since 2017-12-29
 */
@Controller
@RequestMapping("/insure/")
public class TbInsureController extends BaseController {

    @Autowired
    private ITbInsureService insureService;

    @GetMapping("listUI")
    public String listUI() {
        return "insure/list";
    }

    @SuppressWarnings("unchecked")
    @PostMapping("list")
    @ResponseBody
    public Object list(String gridPager) {
        Pager pager = JsonUtil.getObjectFromJson(gridPager, Pager.class);
        Map<String, Object> parameters = null;
        if(Validator.isNullOrEmpty(pager.getParameters())){
            parameters = new HashMap<>();
        }else{
            parameters = pager.getParameters();
        }
        Page<TbInsure> list = insureService.selectPage(new Page<TbInsure>(pager.getNowPage(), pager.getPageSize()), Condition.create().allEq(parameters).orderBy("id",false));
        makeGridResult(parameters, pager, list);
        return parameters;
    }

    @GetMapping("form")
    public String form(Map<String,Object> map) {
        return "insure/form";
    }

    @RequestMapping(value="all",method= RequestMethod.POST)
    @ResponseBody
    public AbstractBean all(String phone) {
        AbstractBean bean = new AbstractBean();
        List<TbInsure> list = insureService.selectByMap(ConvertUtil.toMap("phone",phone));

        if(list==null){
            bean.setStatus(EnumSvrResult.ERROR.getVal());
            bean.setMessage(EnumSvrResult.ERROR.getName());
        }else{
            bean.setData(list);
        }
        return bean;
    }


    @PostMapping("save")
    @ResponseBody
    public AbstractBean add(TbInsure tbinsure){
        AbstractBean bean = new AbstractBean();
        if (tbinsure.getId() == null) {
            tbinsure.setResBuyDate(new Date());
        }
        if(!insureService.insertOrUpdate(tbinsure)){
            bean.setStatus(EnumSvrResult.ERROR.getVal());
            bean.setMessage(EnumSvrResult.ERROR.getName());
        }
        return bean;
    }

    @DeleteMapping("{id}/delete")
    @ResponseBody
    public AbstractBean delete(@PathVariable(required=true) Integer id) {
        if(!insureService.deleteById(id)){
            return fail(EnumSvrResult.ERROR);
        }
        return success();
    }

    @GetMapping("{id}/select")
    public String select(Map<String,Object> map,@PathVariable(required=true) Integer id) {
        TbInsure tbinsure = insureService.selectById(id);
        map.put("record", tbinsure);
        return "insure/edit";
    }

    @GetMapping("{id}/info")
    public String info(Map<String,Object> map,@PathVariable(required=true) Integer id) {
        TbInsure tbinsure = insureService.selectById(id);
        map.put("record", tbinsure);
        return "machineryInfo/info";
    }

    @DeleteMapping("{ids}/deleteBatch")
    @ResponseBody
    public AbstractBean deleteBatch(@PathVariable(required=true) String ids) {
        if(!insureService.deleteBatchIds(ConvertUtil.toList(ids.split(",")))){
            return fail(EnumSvrResult.ERROR);
        }
        return success();
    }
}
